package com.application.Tree;


import java.util.ArrayList;

import com.application.Activity.R;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnFocusChangeListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;


public class MyDialog extends Dialog implements
		android.view.View.OnClickListener {
	
	private String defautTitle="[Nhập nội dung]";
	public static String defaultText="[Ghi chú]";
	private String titleText = defautTitle;
	private String text = defaultText;
	private int color;
	private int emoticon;
	private String symbol;
	boolean okClick = false;
	private boolean flagUpdateEmoticon = false;
	Button btnOK, btnCancel;
	EditText txtText, txtTitle;
	MyHandlerInterface OKClickListener;
	MyHandlerInterface CancelClickListener;
	INode myObj;	
    
	public MyDialog(Context context,String title) {
		super(context);
		this.setContentView(R.layout.menu_thao_tac_node);	
		Log.d("An","Mydialog-FlagEmoticon: "+ flagUpdateEmoticon);
		this.setTitle(title);
		txtText = (EditText) findViewById(R.id.txtNoiDung);
		txtTitle = (EditText) findViewById(R.id.txtTitle);
		txtText.setText(this.text);
		txtTitle.setText(this.titleText);
				
		
		Spinner mySpinner = (Spinner) this.findViewById(R.id.spColor);
		final Emoticon[] emo = Emoticon.init();
		final SpinerEmoticonAdapter adapter = new SpinerEmoticonAdapter(context, R.layout.spiner_view, emo);
		mySpinner.setAdapter(adapter);
		mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				Emoticon emo = adapter.getItem(position); 
				emoticon = emo.emoticon;
				symbol = emo.symbols;
				String s = txtTitle.getText().toString();
				if(symbol != " "){
					String e = "'"+symbol+"'";					
					txtTitle.setText(s + e);				
				}
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		btnCancel = (Button) findViewById(R.id.btnCancel);
		btnOK = (Button) findViewById(R.id.btnOK);
		
		btnCancel.setOnClickListener(this);
		btnOK.setOnClickListener(this);

		btnOK.setText("Đồng ý");
		btnCancel.setText("Hủy bỏ");
		
		txtText.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(hasFocus){
					if (txtText.getText().toString().equals(defaultText))
						txtText.setText("");
				}else{
					
					String s=txtText.getText().toString().replace("\\s", "");
					if(s.length()==0)	{
						txtText.setText(defaultText);
					}
				}

			}
		});
		txtTitle.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (txtTitle.getText().toString().equals(defautTitle)){
					txtTitle.setText("");
				}else{
					
					String s=txtTitle.getText().toString().replace("\\s", "");
					if(s.length()==0)	{
						txtTitle.setText(defautTitle);
					}
				}

			}
		});
	}
	
	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.btnOK:
			this.text = txtText.getText().toString();
			this.titleText = txtTitle.getText().toString();
			this.okClick = true;
			if(OKClickListener!=null)
		         OKClickListener.onHandle(myObj);
			break;

		default:
			this.okClick = false;
			if(CancelClickListener!=null){
				CancelClickListener.onHandle(myObj);
			}
			break;
		}
		dismiss();
	}
	

	public void setOKClickListener(MyHandlerInterface oKClickListener) {
		OKClickListener = oKClickListener;
	}

	public void setCancelClickListener(MyHandlerInterface cancelClickListener) {
		CancelClickListener = cancelClickListener;
	}

	public String getTextTitle() {
		return titleText;
	}

	public void setTextTitle(String title) {
		this.titleText = title;
		this.txtTitle.setText(title);
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
		this.txtText.setText(text);
	}

	public int getColor() {
		return color;
	}

	public void setColor(int color) {
		this.color = color;
	}

	public boolean isOkClick() {
		return okClick;
	}

	public void setOkClick(boolean okClick) {
		this.okClick = okClick;
	}

	public int getEmoticon() {
		return emoticon;
	}

	public void setEmoticon(int emoticon) {
		this.emoticon = emoticon;
	}
	

	public boolean isFlagEmoticon() {
		return flagUpdateEmoticon;
	}

	public void setFlagEmoticon(boolean flagEmoticon) {
		this.flagUpdateEmoticon = flagEmoticon;
	}
	
}
